package com.app.mingluxing.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.app.mingluxing.R;
import com.app.mingluxing.bean.InviteBean;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.joooonho.SelectableRoundedImageView;

import java.util.List;

/**
 * 好友请求adapter
 * Created by Aaron on 16/2/22.
 */
public class NewFriendsAdapter extends BaseAdapter {

    private Context ctx;
    private List<InviteBean.InvitedListEntity> invitedList;

    public NewFriendsAdapter(Context ctx, List<InviteBean.InvitedListEntity> invitedList) {
        this.ctx = ctx;
        this.invitedList = invitedList;
    }

    public void setInvitedList(List<InviteBean.InvitedListEntity> invitedList) {
        this.invitedList = invitedList;
    }

    @Override
    public int getCount() {
        return invitedList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_new_friends, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_head_pic = (SelectableRoundedImageView) convertView.findViewById(R.id.iv_head_pic);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            viewHolder.tv_agree = (TextView) convertView.findViewById(R.id.tv_agree);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (invitedList == null || invitedList.get(position) == null || invitedList.get(position).user == null) {
            return convertView;
        }

        Glide.with(ctx)
                .load(invitedList.get(position).user.imgUrl)
                .dontAnimate().diskCacheStrategy(DiskCacheStrategy.SOURCE).fitCenter()
                .placeholder(R.mipmap.temp_pic).into(viewHolder.iv_head_pic);//聊天头像

        viewHolder.tv_title.setText(invitedList.get(position).user.nickName);
        viewHolder.tv_content.setText(invitedList.get(position).inviteWord);

        switch (invitedList.get(position).state) {

            case 0://未处理 待同意

                viewHolder.tv_agree.setText("操作");
                viewHolder.tv_agree.setTextColor(ctx.getResources().getColor(R.color.white));
                viewHolder.tv_agree.setBackgroundResource(R.drawable.shape_blue_comment);

//                setClickTrue(viewHolder.tv_agree);

                break;
            case -1://已拒绝

                viewHolder.tv_agree.setText("已拒绝");
                viewHolder.tv_agree.setTextColor(ctx.getResources().getColor(R.color.text_color));
                viewHolder.tv_agree.setBackgroundColor(ctx.getResources().getColor(R.color.transparent));


//                setClickFalse(viewHolder.tv_agree);

                break;
            case 1://已同意
                viewHolder.tv_agree.setText("已同意");
                viewHolder.tv_agree.setTextColor(ctx.getResources().getColor(R.color.text_color));
                viewHolder.tv_agree.setBackgroundColor(ctx.getResources().getColor(R.color.transparent));

//                setClickFalse(viewHolder.tv_agree);

                break;

        }

//        //同意
//        viewHolder.tv_agree.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                if (ctx instanceof ChatAgreeActivity) {
//
//                    NewFriendsActivity agreeActivity = (NewFriendsActivity) ctx;
//
//                    agreeActivity.handleFriendRequest(invitedList.get(position).user.phoneNum, new AddSuccessListener() {
//                        @Override
//                        public void onSuccessListener() {
//
//                            invitedList.get(position).state = 1;
//                            notifyDataSetChanged();
//
//                        }
//                    });
//
//                }
//
//            }
//        });

        return convertView;
    }

//
//    public interface AddSuccessListener {
//
//        void onSuccessListener();
//
//    }


//    private void setClickFalse(TextView textView) {
//        textView.setClickable(false);
//        textView.setFocusable(false);
//        textView.setEnabled(false);
//    }
//
//    private void setClickTrue(TextView textView) {
//        textView.setClickable(true);
//        textView.setFocusable(true);
//        textView.setEnabled(true);
//    }

    private class ViewHolder {
        private SelectableRoundedImageView iv_head_pic;
        private TextView tv_content;
        private TextView tv_title;
        private TextView tv_agree;

    }

}
